Completed
Push — master ( 641a91...fc9f6a )
by
unknown
10:16
created

angular.controller(ꞌGenericCsvImportCtrlꞌ)   B

Complexity

Conditions 1
Paths 4

Size

Total Lines 171

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 171
rs 7
c 0
b 0
f 0
nc 4
nop 3

7 Functions

Rating   Name   Duplication   Size   Complexity  
A ��) 0 3 1
A ��) 0 4 2
A ��) 0 3 1
B ��) 0 26 8
A ��) 0 36 2
A ��) 0 33 1
A ��) 0 6 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
(function () {
24
	'use strict';
25
26
27
	/**
28
	 * @ngdoc function
29
	 * @name passmanApp.controller:MenuCtrl
30
	 * @description
31
	 * # MenuCtrl
32
	 * Controller of the passmanApp
33
	 */
34
	angular.module('passmanApp')
35
		.controller('GenericCsvImportCtrl', ['$scope', 'CredentialService', '$translate',
36
			function ($scope, CredentialService, $translate) {
37
				$scope.hello = 'world';
38
39
				$scope.credentialProperties = [
40
					{
41
						label: 'Label',
42
						prop: 'label',
43
						matching: ['label', 'title', 'name']
44
					},
45
					{
46
						label: 'Username',
47
						prop: 'username',
48
						matching: ['username', 'user', 'login', 'login name']
49
					},
50
					{
51
						label: 'Password',
52
						prop: 'password',
53
						matching: ['password', 'pass', 'pw']
54
					},
55
					{
56
						label: 'TOTP Secret',
57
						prop: 'otp',
58
						matching: ['totp']
59
					},
60
					{
61
						label: 'Custom field',
62
						prop: 'custom_field'
63
					},
64
					{
65
						label: 'Notes',
66
						prop: 'description',
67
						matching: ['notes', 'description', 'comments']
68
					},
69
					{
70
						label: 'Email',
71
						prop: 'email',
72
						matching: ['email', 'mail']
73
					},
74
					{
75
						label: 'URL',
76
						prop: 'url',
77
						matching: ['website', 'url', 'fulladdress', 'site', 'web site']
78
					},
79
					{
80
						label: 'Tags',
81
						prop: 'tags'
82
					},
83
					{
84
						label: 'Ignored',
85
						prop: null
86
					}
87
				];
88
				var tagMapper = function (t) {
89
					return {text: t};
90
				};
91
				var rowToCredential = function (row) {
92
					var _credential = PassmanImporter.newCredential();
93
					for(var k = 0; k < $scope.import_fields.length; k++){
94
						var field = $scope.import_fields[k];
95
						if(field){
96
							if(field === 'otp'){
97
								_credential.otp.secret = row[k];
98
							} else if(field === 'custom_field'){
99
								var key = ($scope.matched) ? $scope.parsed_csv[0][k] : 'Custom field '+ k;
100
								_credential.custom_fields.push({
101
									'label': key,
102
									'value': row[k],
103
									'secret': 0
104
								});
105
							} else if(field === 'tags'){
106
								if( row[k]) {
107
									var tags = row[k].split(',');
108
									_credential.tags = tags.map(tagMapper);
109
								}
110
							} else{
111
								_credential[field] = row[k];
112
							}
113
						}
114
					}
115
					return _credential;
116
				};
117
118
119
				$scope.inspectCredential = function (row) {
120
					$scope.inspected_credential = rowToCredential(row);
121
				};
122
123
				$scope.csvLoaded = function (file) {
124
					$scope.import_fields = [];
125
					$scope.inspected_credential = false;
126
					$scope.matched = false;
127
					$scope.skipFirstRow = false;
128
					var file_data = file.data.split(',');
129
					file_data = decodeURIComponent(escape(window.atob(file_data[1])));
130
					/** global: Papa */
131
					Papa.parse(file_data, {
132
						complete: function(results) {
133
							if(results.data) {
134
								for(var i = 0; i < results.data[0].length; i++){
135
									var propName = results.data[0][i];
136
									$scope.import_fields[i] = null;
137
									for(var p = 0; p < $scope.credentialProperties.length; p++){
138
										var credentialProperty = $scope.credentialProperties[p];
139
										if(credentialProperty.matching){
140
											if(credentialProperty.matching.indexOf(propName.toLowerCase()) !== -1){
141
												$scope.import_fields[i] = credentialProperty.prop;
142
												$scope.matched = true;
143
											}
144
										}
145
									}
146
								}
147
								if($scope.matched){
148
									$scope.inspectCredential(results.data[1]);
149
								}
150
								$scope.parsed_csv = results.data;
151
								$scope.$apply();
152
							}
153
						}
154
					});
155
				};
156
157
				var addCredential = function (index) {
158
					function handleState (index) {
159
						if ($scope.parsed_csv[index + 1]) {
160
							$scope.import_progress = {
161
								progress: index / $scope.parsed_csv.length * 100,
162
								loaded: index,
163
								total: $scope.parsed_csv.length
164
							};
165
166
							addCredential(index + 1);
167
						} else {
168
							$scope.import_progress = {
169
								progress: 100,
170
								loaded: $scope.parsed_csv.length,
171
								total: $scope.parsed_csv.length
172
							};
173
							$scope.log.push($translate.instant('done'));
174
							$scope.importing = false;
175
						}
176
					}
177
178
					var _credential = rowToCredential($scope.parsed_csv[index]);
179
					_credential.vault_id = $scope.active_vault.vault_id;
180
					if (!_credential.label) {
181
						$scope.log.push($translate.instant('import.skipping', {line: index}));
182
						handleState(index);
183
						return;
184
					}
185
					$scope.log.push($translate.instant('import.adding', {credential: _credential.label}));
186
					CredentialService.createCredential(_credential).then(function (result) {
187
						if (result.credential_id) {
188
							$scope.log.push($translate.instant('import.added', {credential: _credential.label}));
189
							handleState(index);
190
						}
191
					});
192
				};
193
194
				$scope.importing = false;
195
				$scope.startCSVImport = function () {
196
					$scope.importing = true;
197
					$scope.log = [];
198
					var start = ($scope.skipFirstRow) ? 1 : 0;
199
					addCredential(start);
200
				};
201
202
				$scope.updateExample = function () {
203
					var start = ($scope.skipFirstRow) ? 1 : 0;
204
					$scope.inspectCredential($scope.parsed_csv[start]);
205
				};
206
			}]);
207
}());